package com.pesking.core.sqlparser.processor;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.pesking.core.exception.ParameterException;
import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.thread.ThreadManage;

public class RequestHeaderParameterSqlHandler extends SqlParameterProcessor {
	@Override
	public String getTitle() {
		return "请求头参数";
	}

	@Override
	public String getDescription() {
		return this.getTitle() + " 根据请求头名替换到指定位置";
	}

	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		String name = sqlParameter.getName();
		String defaultValue = sqlParameter.getValue();
		if (StringUtils.isBlank(name)) {
			throw new ParameterException("参数配置错误,使用方法：[REQUEST_HEADER:名称:缺省值]");
		}
		String value = ThreadManage.getCurrentRequest().getHeader(name);
		if (StringUtils.isBlank(value)) {
			value = defaultValue;
		}
		if (value == null) {
			value = "";
		}
		sbSQL.append(value);
	}

	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"REQUEST_HEADER"});
	}

}
